package com.zret.oasys.operation.emp;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zret.oasys.dbtools.MD5Util;
import com.zret.oasys.entity.Emp;
import jdk.internal.org.objectweb.asm.tree.analysis.Value;

public class LoginEmp implements EmpServletOperation {

	@Override
	public void doOperation(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		
		String username=request.getParameter("username");
		String password= MD5Util.getEncodeByMd5(request.getParameter("password"));
		List<Emp> list =empService.GetLogin(username, password);
		if(list.size()!=0) {
			//把session存到Map里
			HashMap hashMap = new HashMap();
			hashMap.put("empid", list.get(0).getEmpid());
			hashMap.put("empname", list.get(0).getEmpname());
			hashMap.put("position", list.get(0).getPosition());
			request.getSession().setAttribute("emp", hashMap);
			if(request.getParameter("cksremember")!=null) {
				//添加cookie 并设置时间
				Cookie cookie1 =new Cookie("username", username);
				cookie1.setMaxAge(60*60);
				Cookie cookie2 =new Cookie("password",request.getParameter("password"));
				cookie2.setMaxAge(60*60);
				response.addCookie(cookie1);
				response.addCookie(cookie2);
				System.out.println(cookie1);
				System.out.println(cookie2);
			
			}
			response.sendRedirect("Note.jsp");
		}else {
			response.sendRedirect("login.jsp");
		}

	}

}
